package com.ruoyi.jiajia.controller;

import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.system.domain.JiajiaHospital;
import com.ruoyi.system.domain.JiajiaItemType;
import com.ruoyi.system.domain.JiajiaWorkerItem;
import com.ruoyi.system.service.IJiajiaHospitalService;
import com.ruoyi.system.service.IJiajiaItemTypeService;
import org.apache.commons.compress.utils.Lists;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.JiajiaItem;
import com.ruoyi.system.service.IJiajiaItemService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目Controller
 * 
 * @author knd
 * @date 2021-10-13
 */
@Controller
@RequestMapping("/jiajia/item")
public class JiajiaItemController extends BaseController
{
    private String prefix = "jiajia/item";

    @Autowired
    private IJiajiaItemService jiajiaItemService;
    @Autowired
    private IJiajiaItemTypeService jiajiaItemTypeService;



    @RequiresPermissions("jiajia:item:view")
    @GetMapping()
    public String item(ModelMap mmap)
    {
        List<JiajiaItemType> itemTypeList = jiajiaItemTypeService.selectJiajiaItemTypeList(new JiajiaItemType());
        mmap.put("itemTypeList", itemTypeList);
        return prefix + "/item";
    }

    /**
     * 查询项目列表
     */
    @RequiresPermissions("jiajia:item:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JiajiaItem jiajiaItem)
    {
        startPage();
        List<JiajiaItem> list = jiajiaItemService.selectJiajiaItemList(jiajiaItem);
        return getDataTable(list);
    }

    /**
     * 导出项目列表
     */
    @RequiresPermissions("jiajia:item:export")
    @Log(title = "项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JiajiaItem jiajiaItem)
    {
        List<JiajiaItem> list = jiajiaItemService.selectJiajiaItemList(jiajiaItem);
        ExcelUtil<JiajiaItem> util = new ExcelUtil<JiajiaItem>(JiajiaItem.class);
        return util.exportExcel(list, "项目数据");
    }

    /**
     * 新增项目
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<JiajiaItemType> itemTypeList = jiajiaItemTypeService.selectJiajiaItemTypeList(new JiajiaItemType());
        mmap.put("itemTypeList", itemTypeList);
        return prefix + "/add";
    }

    /**
     * 新增保存项目
     */
    @RequiresPermissions("jiajia:item:add")
    @Log(title = "项目", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JiajiaItem jiajiaItem)
    {

        long count = PageHelper.count(new ISelect() {
            @Override
            public void doSelect() {
                JiajiaItem item = new JiajiaItem();
                item.getParams().put("name",jiajiaItem.getName());
                jiajiaItemService.selectJiajiaItemList(item);
            }
        });
        if (count > 0){
            return AjaxResult.error("项目名称重复");
        }


        if(jiajiaItem.getTimeSpan() == null||jiajiaItem.getTimeSpan().intValue() <= 0){
            return AjaxResult.error("项目时长不能为零");
        }

        if(jiajiaItem.getPrice() == null||jiajiaItem.getPrice().floatValue() <= 0){
            return AjaxResult.error("项目原价不能为零");
        }

        if(jiajiaItem.getDisPrice() == null || jiajiaItem.getDisPrice().floatValue() <= 0){
            return AjaxResult.error("项目现价不能为零");
        }
        return toAjax(jiajiaItemService.insertJiajiaItem(jiajiaItem));
    }

    /**
     * 修改项目
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        JiajiaItem jiajiaItem = jiajiaItemService.selectJiajiaItemById(id);
        mmap.put("jiajiaItem", jiajiaItem);
        List<JiajiaItemType> itemTypeList = jiajiaItemTypeService.selectJiajiaItemTypeList(new JiajiaItemType());
        List<JiajiaItemType> itemTypes = itemTypeList.stream().filter(rowVal -> StrUtil.equals(rowVal.getCode(), jiajiaItem.getTypeCode())).collect(Collectors.toList());
        if(ObjectUtil.isNotEmpty(itemTypes)){
            itemTypes.get(0).setSelected(true);
        }
        mmap.put("itemTypeList", itemTypeList);
        return prefix + "/edit";
    }

    /**
     * 修改保存项目
     */
    @RequiresPermissions("jiajia:item:edit")
    @Log(title = "项目", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JiajiaItem jiajiaItem)
    {

        if(jiajiaItem.getTimeSpan()==null||jiajiaItem.getTimeSpan().intValue()<=0){
            return AjaxResult.error("项目时长不能为零");
        }

        if(jiajiaItem.getPrice()==null||jiajiaItem.getPrice().floatValue()<=0){
            return AjaxResult.error("项目原价不能为零");
        }

        if(jiajiaItem.getDisPrice()==null||jiajiaItem.getDisPrice().floatValue()<=0){
            return AjaxResult.error("项目现价不能为零");
        }

        return toAjax(jiajiaItemService.updateJiajiaItem(jiajiaItem));
    }

    /**
     * 删除项目
     */
    @RequiresPermissions("jiajia:item:remove")
    @Log(title = "项目", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jiajiaItemService.deleteJiajiaItemByIds(ids));
    }

    /**
     * 项目详情
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap) {
        mmap.put("id",id);
        JiajiaItem item = jiajiaItemService.selectJiajiaItemById(id);
        mmap.put("itemInfo",item);



        return prefix + "/detail";
    }
}
